package com.download.web.login;

import com.blade.exception.ValidatorException;
import com.download.web.api.ApiRequestService;
import com.download.web.entity.model.MsgPushEntity;
import com.download.web.pay.MakeMoneyService;
import com.download.web.pay.enums.PlatFormEnum;
import com.download.web.service.ISysThirdAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 登录
 * @author: cmy
 * @create: 2019-03-01 18:31
 **/
@Slf4j
@Controller
@RequestMapping("/login")
public class LoginController {



    @Autowired
    private ISysThirdAccountService thirdAccountService;


    //查询qq登录接口需要的参数值
//    @RequestMapping("/authorParam")
//    public void qqLogin(){
//
//    }
    //qq第三方登录授权回调
//    @ResponseBody
    @RequestMapping("/qqAuthorizeCallback")
    public ModelAndView qqAuthorizeCallback(String code){
        ModelAndView mv=new ModelAndView();
        Map<String, Object> weiboUserMap=thirdAccountService.qqLoginCallback(code);
        if (null!=weiboUserMap.get("user")){
            mv.setViewName("index");
        }else {
            mv.setViewName("/web/thirdregister");
        }
        mv.addObject("data",weiboUserMap);
        return mv;
    }


    //微博第三方登录授权回调
//    @RequestMapping("/weiboUserToken")
//    public void weiboUserToken(){
//        System.out.println("");
//    }

    //微博第三方登录授权回调
    @RequestMapping("/weiboAuthorizeCallback")
    public ModelAndView weiboAuthorizeCallback(String code){
        ModelAndView mv=new ModelAndView();
        Map<String, Object> weiboUserMap=thirdAccountService.weiboLogin(code);
        if (null!=weiboUserMap.get("user")){
            mv.setViewName("index");
        }else {
            mv.setViewName("/web/thirdregister");
        }
        mv.addObject("data",weiboUserMap);
        return mv;
    }

    @Autowired
    private MakeMoneyService makeMoneyService;

    @RequestMapping("/pay/youzan/payCallback")
    public Map<String,Object> payCallback(HttpServletRequest request,@RequestBody MsgPushEntity entity) {
        Map<String, String[]> params = request.getParameterMap();
        try {
//            Validator.orderParam(order);
           /* if (null == request.header("") || null == request.userAgent()) {
                return RestResponse.fail("非法请求");
            }*/
            String keyWord = URLDecoder.decode(entity.getMsg(), "utf-8");
            makeMoneyService.execCallback(PlatFormEnum.YOUZAN, entity.getMsg());
            Map<String,Object> map=new HashMap<>();
            map.put("code",0);
            map.put("msg","success");
            return map;
        } catch (Exception e) {
            if (!(e instanceof ValidatorException)) {
                log.error("支付出现异常", e);
            }
//            return RestResponse.fail(e.getMessage());
            return null;
        }
    }


}
